//
//  MyTabBarViewController.m
//  zhuayin
//
//  Created by wj on 16/6/30.
//  Copyright © 2016年 wujian. All rights reserved.
//

#import "MyTabBarViewController.h"
#import "UserNavigationController.h"
#import "FindViewController.h"
#import "ChaseBookViewController.h"
#import "BookCityViewController.h"
#import "MyViewController.h"
#import "MyTabBar.h"

@interface MyTabBarViewController ()<UITabBarDelegate,UITabBarControllerDelegate>{
    
}

@property (nonatomic, assign) NSInteger currentSelectedIndex;  //当前选中的tabbar索引 默认-1

@property (nonatomic,strong)UserNavigationController *nav;

@end

@implementation MyTabBarViewController

+(void)initialize{
    
    // 通过appearance统一设置所有UITabBarItem的文字属性
    UITabBarItem *tabBarItem = [UITabBarItem appearance];
    
    /** 设置默认状态 */
    NSMutableDictionary *norDict = @{}.mutableCopy;
    norDict[NSFontAttributeName] = [UIFont systemFontOfSize:kFontValue(10.0)];
    norDict[NSForegroundColorAttributeName] = CSColorFromRGB(0x6B6763);
    [tabBarItem setTitleTextAttributes:norDict forState:UIControlStateNormal];
    
    /** 设置选中状态 */
    NSMutableDictionary *selDict = @{}.mutableCopy;
    selDict[NSFontAttributeName] = [UIFont systemFontOfSize:kFontValue(10.0)];
    selDict[NSForegroundColorAttributeName] = CSColorFromRGB(0xF52C50);
    [tabBarItem setTitleTextAttributes:selDict forState:UIControlStateSelected];
    
    //[tabBarItem setTitlePositionAdjustment:UIOffsetMake(0, -12)];
    
}

-(id)init{
    
    self = [super init];
    
    if (self) {
        
        [self setUp];
    }
    
    return self;
}


-(void)setUp{
    
    /*添加子控制器 */
    
    /** 发现*/
    FindViewController *findVC = [[FindViewController alloc] init];
    [self setUpChildControllerWith:findVC norImage:[UIImage imageNamed:@"发现灰"] selImage:[UIImage imageNamed:@"发现"] title:@"发现"];
    
    /** 追书 */
    ChaseBookViewController *chaseBookVC = [[ChaseBookViewController alloc] init];
    [self setUpChildControllerWith:chaseBookVC norImage:[UIImage imageNamed:@"追书灰"] selImage:[UIImage imageNamed:@"追书"] title:@"追书"];
    
//    NSDictionary *dict =  [kUserDefault objectForKey:@"StudioDict"];
//
//
//    BOOL isHost = [[dict objectForKey:@"IsHost"] boolValue];
//
//    if (isHost) {
//
//        /** 统计 */
//        StatisticsViewController *statisticsVC = [[StatisticsViewController alloc] init];
//        [self setUpChildControllerWith:statisticsVC norImage:[UIImage imageNamed:@"tab_statistical_n"] selImage:[UIImage imageNamed:@"tab_statistical_s"] title:@"统计"];
//    }
    
    /** 书城 */
    BookCityViewController *bookCityVC = [[BookCityViewController alloc] init];
    [self setUpChildControllerWith:bookCityVC norImage:[UIImage imageNamed:@"书城灰"] selImage:[UIImage imageNamed:@"书城"] title:@"书城"];
    
    MyTabBar *tabBar = [[MyTabBar alloc] init];
        
    tabBar.delegate = self;
    
    //tabBar.myTabBarDelegate = self;
    
    // KVC：如果要修系统的某些属性，但被设为readOnly，就是用KVC，即setValue：forKey：。
    // 修改tabBar为自定义tabBar
    [self setValue:tabBar forKey:@"tabBar"];
            
    self.tabBar.barTintColor = CSColorFromRGB(0xFFFFFF);//背景颜色
    
    self.tabBar.translucent = NO;
        
    //去掉tabBar顶部线条
    CGRect rect = CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    //CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
    CGContextSetFillColorWithColor(context, [LineBackGroundColor CGColor]);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [self.tabBar setShadowImage:img];
}

//设置状态栏颜色
-(void)viewWillAppear:(BOOL)animated{

    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}

-(BOOL)prefersStatusBarHidden{
    return NO;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.delegate = self;
    
    //1.设置默认值以及绑定代理
    self.currentSelectedIndex = -1;
}

- (void)setUpChildControllerWith:(UIViewController *)childVc norImage:(UIImage *)norImage selImage:(UIImage *)selImage title:(NSString *)title
{
        
    childVc.title = title;
    childVc.tabBarItem.title = title;
    childVc.tabBarItem.image = [norImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childVc.tabBarItem.selectedImage = [selImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];    
    UserNavigationController *nav = [[UserNavigationController alloc] initWithRootViewController:childVc];
    [self addChildViewController:nav];
    
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    
    NSUInteger tabIndex = [tabBar.items indexOfObject:item];
    
    NSLog(@"tabIndex = %lu",(unsigned long)tabIndex);
    
    if (tabIndex >= 2) {
        
        tabIndex = tabIndex + 1;
    }

    
    //未登录
    if (tabIndex > 0 && ![kUserDefault boolForKey:@"isLogin"]) {
                
        return;
    }
    
    MyTabBar *myTabBar = (MyTabBar *)tabBar;
    
    [myTabBar setSelectIndex:tabIndex];
}


#pragma mark UITabBarControllerDelegate

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    
}



/**
 加载数据 是管理员还是教员
 */
-(void)loadData{
    
    if ([kUserDefault boolForKey:@"isLogin"]) {
        
        NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
        
        [params setValue:[kUserDefault objectForKey:@"userID"]  forKey:@"ID"];
        
        [params setValue:[kUserDefault objectForKey:@"userID"]  forKey:@"SelfID"];
        
//        [HTTPTool GET:Get_Users parameters:params
//         
//              success:^(id responseObject){
//                  
//              }
//         
//              failure:^(NSError *error){
//                  
//              }];
    }
}

-(void)dealloc{
    
    [NotificationCenter removeObserver:self];
}

@end
